package com.supersit.GZFDA.view;

import com.supersit.GZFDA.R;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class LoadingDialog {

	public Dialog dialog;
	private TextView message, submit;
	private ProgressBar progressBar;
	private View line;

	/*
	 * public static LoadingDialog instance=null;
	 * 
	 * public static LoadingDialog getInstance(){ if(instance==null){
	 * synchronized (LoadingDialog.class) { if(instance==null){ instance=new
	 * LoadingDialog(); } } } return instance; }
	 */

	public LoadingDialog(Context context) {
		dialog = new Dialog(context, R.style.Loading_dialog);
		dialog.setContentView(R.layout.loading_dialog);
		dialog.setCancelable(false);
		dialog.setCanceledOnTouchOutside(false);
		progressBar = (ProgressBar) dialog.findViewById(R.id.loadint_pro);
		message = (TextView) dialog.findViewById(R.id.loading_message);
		line = dialog.findViewById(R.id.loading_line);
		submit = (TextView) dialog.findViewById(R.id.loading_message_submit);
	}

	public void showLoadingDialog(String showMessage) {
		try {
			dialog.dismiss();
			dialog.setCancelable(false);
			dialog.setCanceledOnTouchOutside(false);
			progressBar.setVisibility(View.VISIBLE);
			message.setVisibility(View.VISIBLE);
			submit.setVisibility(View.GONE);
			line.setVisibility(View.GONE);
			message.setText(showMessage);
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
			dismissLoadDialog();
		}
	}

	public void showLoadedAndDismissDialog(Context context, String showMessage,
			long delayTime, final boolean isFinishActivity,
			final Activity activity) {
		// dialog.dismiss();
		try {
			dismissLoadDialog();
			line.setVisibility(View.GONE);
			submit.setVisibility(View.GONE);
			progressBar.setVisibility(View.GONE);
			message.setText(showMessage);
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				// dialog.dismiss();
				dismissLoadDialog();
				// if (isFinishActivity) {
				// // activity.finish();
				// MainApp.getInstance().removeActivity(activity);
				// }
			}
		}, delayTime);
	}

	public void showLoadedEventDialog(String showMessage, String showMessage2,
			OnClickListener listen, OnClickListener listener) {
		dismissLoadDialog();
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(true);
		progressBar.setVisibility(View.GONE);
		if (TextUtils.isEmpty(showMessage)) {
			message.setVisibility(View.GONE);

		} else {
			message.setText(showMessage);

		}
		try {
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
		submit.setVisibility(View.VISIBLE);
		if (TextUtils.isEmpty(showMessage2)) {
			submit.setVisibility(View.GONE);
			line.setVisibility(View.GONE);
		} else {

			submit.setText(showMessage2);
			line.setVisibility(View.VISIBLE);
		}
		message.setOnClickListener(listen);
		submit.setOnClickListener(listener);
	}

	public void dismissLoadDialog() {
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}
	}
}
